package com.nofeng.wx.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.nofeng.wx.dao.entity.AppConfig;
import com.nofeng.wx.dao.mapper.AppConfigMapper;
import com.nofeng.wx.service.appium.AppiumService;
import com.plugin.dao.annocation.ShardingStrategy;
import io.appium.java_client.android.AndroidDriver;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@Service
public class MobileAppService {

    public String getPageSource(){
        try {
            AppiumService.init("http://8.138.24.62:4723", "com.lphtsccft", ".zhangle.main.MainActivity");
            String pageSource = AppiumService.getDriver().getPageSource();
            System.out.println(pageSource);
            return pageSource;
        }catch (Exception e){
            e.printStackTrace();
        }
        return "ERROR";
    }

    public void stop(){
        String appPackage = "com.eg.android.AlipayGphone";
        if(null!=AppiumService.geThreadManager()){
            AppiumService.geThreadManager().shutdownNow(androidDriver -> {
                androidDriver.terminateApp(appPackage);
            });
        }
    }

    public  Map<String, Object>  reStart(){
        String appPackage = "com.eg.android.AlipayGphone";
        return AppiumService.restart("http://8.138.24.62:4723", appPackage, ".AlipayLogin");

    }
    public void ali(){
        String appPackage = "com.eg.android.AlipayGphone";
        if(null!=AppiumService.geThreadManager()){
            stop();
        }
        AppiumService.init("http://8.138.24.62:4723", appPackage, ".AlipayLogin");
        AndroidDriver driver = AppiumService.getDriver();
        AppiumService.AppScript testScript = new AppiumService.AppScript();
        testScript.setApp(appPackage);
        testScript.setReActivateApp(true);
        testScript.setEvent(Lists.newArrayList());
        testScript.getEvent().add(new AppiumService.AppScript.AppClickAndWaitEvent("//android.widget.TextView[@resource-id=\"com.alipay.android.tablauncher:id/tab_description\" and @text=\"视频\"]"));
//        testScript.getEvent().add(new AppiumService.AppScript.AppClickAndWaitEvent("//android.widget.TextView[@resource-id=\"com.lphtsccft:id/func_name\" and @text=\"卖\"]"));
//        testScript.getEvent().add(new AppiumService.AppScript.AppSetValueEvent("//android.widget.EditText[@resource-id=\"com.lphtsccft:id/login_account_edit\"]", "666641539091"));
//        testScript.getEvent().add(new AppiumService.AppScript.AppSetValueEvent("//android.widget.EditText[@resource-id=\"com.lphtsccft:id/login_password_edit\"]", "168168"));
//        testScript.getEvent().add(new AppiumService.AppScript.AppClickAndWaitEvent("//android.widget.Button[@resource-id=\"com.lphtsccft:id/login_btn_login\"]"));
//        testScript.getEvent().add(new AppiumService.AppScript.AppClickAndWaitEvent("//android.widget.EditText[@resource-id=\"com.lphtsccft:id/trade_editText\"]"));
//        testScript.getEvent().add(new AppiumService.AppScript.AppSetValueEvent("//android.widget.EditText[@resource-id=\"com.lphtsccft:id/search_edit_text\"]", code));
//        testScript.getEvent().add(new AppiumService.AppScript.AppSetValueEvent("//android.widget.EditText[@resource-id=\"com.lphtsccft:id/et_content\" and @text=\"卖出价格\"]", price));
//        testScript.getEvent().add(new AppiumService.AppScript.AppClickAndWaitEvent("//android.widget.TextView[@resource-id=\"com.lphtsccft:id/label_name\" and @text=\"" + countDesc + "\"]"));
//
//        testScript.getEvent().add(new AppiumService.AppScript.AppClickAndWaitEvent("//android.widget.TextView[@resource-id=\"com.lphtsccft:id/tv_trade\"]"));
//        //testScript.getEvent().add(new AppScript.AppClickAndWaitEvent("//android.widget.TextView[@resource-id=\"com.lphtsccft:id/tv_positive\"]"));
        //AppiumService.runAppScript(testScript);
        driver.terminateApp(testScript.getApp());
        driver.activateApp(testScript.getApp());
        AppiumService.clickXpathForBackGround("//android.widget.TextView[@resource-id=\"com.alipay.android.tablauncher:id/tab_description\" and @text=\"视频\"]");
        AppiumService.geThreadManager().submitTask(new Runnable() {
            @Override
            public void run() {
                while(true){
                    AppiumService.swipeUp();
                }
            }
        },1000*60*60*4);


    }

    public void s(){
        String appPackage = "com.eg.android.AlipayGphone";
        if(null!=AppiumService.geThreadManager()){
            stop();
        }
        AppiumService.init("http://8.138.24.62:4723", appPackage, ".AlipayLogin");
        AndroidDriver driver = AppiumService.getDriver();
        AppiumService.AppScript testScript = new AppiumService.AppScript();
        testScript.setApp(appPackage);
        testScript.setReActivateApp(true);
        testScript.setEvent(Lists.newArrayList());
        testScript.getEvent().add(new AppiumService.AppScript.AppClickAndWaitEvent("//android.widget.TextView[@resource-id=\"com.alipay.android.tablauncher:id/tab_description\" and @text=\"视频\"]"));
//        testScript.getEvent().add(new AppiumService.AppScript.AppClickAndWaitEvent("//android.widget.TextView[@resource-id=\"com.lphtsccft:id/func_name\" and @text=\"卖\"]"));
//        testScript.getEvent().add(new AppiumService.AppScript.AppSetValueEvent("//android.widget.EditText[@resource-id=\"com.lphtsccft:id/login_account_edit\"]", "666641539091"));
//        testScript.getEvent().add(new AppiumService.AppScript.AppSetValueEvent("//android.widget.EditText[@resource-id=\"com.lphtsccft:id/login_password_edit\"]", "168168"));
//        testScript.getEvent().add(new AppiumService.AppScript.AppClickAndWaitEvent("//android.widget.Button[@resource-id=\"com.lphtsccft:id/login_btn_login\"]"));
//        testScript.getEvent().add(new AppiumService.AppScript.AppClickAndWaitEvent("//android.widget.EditText[@resource-id=\"com.lphtsccft:id/trade_editText\"]"));
//        testScript.getEvent().add(new AppiumService.AppScript.AppSetValueEvent("//android.widget.EditText[@resource-id=\"com.lphtsccft:id/search_edit_text\"]", code));
//        testScript.getEvent().add(new AppiumService.AppScript.AppSetValueEvent("//android.widget.EditText[@resource-id=\"com.lphtsccft:id/et_content\" and @text=\"卖出价格\"]", price));
//        testScript.getEvent().add(new AppiumService.AppScript.AppClickAndWaitEvent("//android.widget.TextView[@resource-id=\"com.lphtsccft:id/label_name\" and @text=\"" + countDesc + "\"]"));
//
//        testScript.getEvent().add(new AppiumService.AppScript.AppClickAndWaitEvent("//android.widget.TextView[@resource-id=\"com.lphtsccft:id/tv_trade\"]"));
//        //testScript.getEvent().add(new AppScript.AppClickAndWaitEvent("//android.widget.TextView[@resource-id=\"com.lphtsccft:id/tv_positive\"]"));
        //AppiumService.runAppScript(testScript);
//        driver.terminateApp(testScript.getApp());
//        driver.activateApp(testScript.getApp());
//        AppiumService.clickXpathForBackGround("//android.widget.TextView[@resource-id=\"com.alipay.android.tablauncher:id/tab_description\" and @text=\"视频\"]");
        AppiumService.geThreadManager().submitTask(new Runnable() {
            @Override
            public void run() {
                while(true){
                    AppiumService.swipeUp();
                }
            }
        });


    }
}
